package com.alibaba.citrus.ots.b2b.trade.order.functions.order.factory;

import com.alibaba.citrus.ots.b2b.trade.order.facade.common.SetOrderTag;
import com.alibaba.citrus.ots.b2b.trade.order.functions.order.entity.OrderWriteEntity;
import com.alibaba.citrus.ots.b2b.trade.order.functions.utils.SaveOrderConverter;
import com.alibaba.citrus.ots.b2b.trade.order.repository.OrderLineQueryServiceRepository;
import com.alibaba.citrus.ots.common.annotation.FunctionInvoker;
import com.alibaba.citrus.ots.common.exception.FunctionException;
import com.alibaba.citrus.ots.common.util.SpringContextUtil;
import com.epoch.app.otsb2btradecenter.dto.request.SaveSaleOrderRequest;
import com.epoch.app.otsb2btradecenter.enums.OrderStatus;
import com.epoch.app.otsb2btradecenter.sdo.OrderLineSDO;
import com.epoch.app.otsb2btradecenter.sdo.OrderSDO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class OrderWriteFactory {

    private static OrderLineQueryServiceRepository orderLineQueryServiceRepository;

    static {
        orderLineQueryServiceRepository = SpringContextUtil.getBean(OrderLineQueryServiceRepository.class);
    }


    public static List<OrderWriteEntity> createOrder(SaveSaleOrderRequest request) {
        List<OrderWriteEntity> orderWriteEntityList = new ArrayList<>();
        for (OrderSDO orderSDO : request.getOrderSDOS()) {
            OrderWriteEntity orderWriteEntity = OrderWriteEntity.of(orderSDO);
            orderWriteEntityList.add(orderWriteEntity);
        }
        return orderWriteEntityList;
    }

    public static OrderWriteEntity updateOrder(OrderSDO orderSDO) {
        return OrderWriteEntity.of(orderSDO);
    }

    public static OrderWriteEntity saveSaleOrder(SaveSaleOrderRequest request) {
        OrderSDO orderSDO = SaveOrderConverter.converterSDO(request);
        return OrderWriteEntity.of(orderSDO);
    }

    public static OrderWriteEntity saveOrderZRE3(OrderSDO orderSDO) {
        return OrderWriteEntity.of(orderSDO);
    }

    @FunctionInvoker
    public static OrderWriteEntity modifyStatus(OrderSDO orderSDO) {
        List<OrderLineSDO> orderLineSDOS = new ArrayList<>();

        //取出含外部行号的子订单
        for ( OrderLineSDO orderLineSDO: orderSDO.getOrderLineSDOS()){
            if (StringUtils.isNotBlank(orderLineSDO.getOuterOrderLineId())) {
                orderLineSDOS.add(orderLineSDO);
            }
        }

        if (CollectionUtils.isEmpty(orderLineSDOS)) {
            throw  new FunctionException("OTS-05-001-10-16-024");
        }

       for ( OrderLineSDO orderLineSDO: orderLineSDOS){
           if (!OrderStatus.COMPLETED.getCode().equals(orderLineSDO.getStatus())) {
               SetOrderTag.setOrderSDOStatus(orderSDO);
               return OrderWriteEntity.of(orderSDO);
           }
       }
        orderSDO.setStatus(OrderStatus.COMPLETED.getCode());

        return OrderWriteEntity.of(orderSDO);
    }

    @FunctionInvoker
    public static OrderWriteEntity modifyChargeAgainstStatus(OrderSDO orderSDO) {

        if (Objects.isNull(orderSDO)){
            throw new FunctionException("OTS-05-001-10-16-026");
        }

        for (OrderLineSDO orderLineSDO : orderSDO.getOrderLineSDOS()) {
            if (StringUtils.isNotBlank(orderLineSDO.getDeliveryAmount())){
                    if (StringUtils.isEmpty(orderSDO.getOrderTag())) {
                        throw new FunctionException("OTS-05-001-10-16-025");
                    }
                    //待交货、部分交货
                    if (orderSDO.getOrderTag().equals(SetOrderTag.NORMAL)) {
                        if (!OrderStatus.BEFORE_DELIVERY.getCode().equals(orderLineSDO.getStatus())) {
                            orderSDO.setStatus(OrderStatus.PARTIAL_DELIVER.getCode());
                            return OrderWriteEntity.of(orderSDO);
                        }
                        orderSDO.setStatus(OrderStatus.BEFORE_DELIVERY.getCode());
                    }
                    //待处理、部分处理
                    if (orderSDO.getOrderTag().equals(SetOrderTag.EXCHANGE)) {
                        //子单类型是换货单
                        if (orderLineSDO.getOrderLineTag().equals(SetOrderTag.EXCHANGE)) {
                            if (!OrderStatus.WAIT_HANDLE.getCode().equals(orderLineSDO.getStatus())) {
                                orderSDO.setStatus(OrderStatus.PARTIAL_HANDLE.getCode());
                                return OrderWriteEntity.of(orderSDO);
                            }
                            orderSDO.setStatus(OrderStatus.WAIT_HANDLE.getCode());
                        }else {
                            //子单是退货类型
                            if (!OrderStatus.WAIT_RECEIVING.getCode().equals(orderLineSDO.getStatus())) {
                                orderSDO.setStatus(OrderStatus.PARTIAL_HANDLE.getCode());
                                return OrderWriteEntity.of(orderSDO);
                            }
                            orderSDO.setStatus(OrderStatus.WAIT_HANDLE.getCode());
                        }

                    }
                    //待收货、部分收货
                    if (orderSDO.getOrderTag().equals(SetOrderTag.RETURN)) {
                        if (!OrderStatus.WAIT_RECEIVING.getCode().equals(orderLineSDO.getStatus())) {
                            orderSDO.setStatus(OrderStatus.PART_RECEIVING.getCode());
                            return OrderWriteEntity.of(orderSDO);
                        }
                        orderSDO.setStatus(OrderStatus.WAIT_RECEIVING.getCode());
                    }

            }

        }
        return OrderWriteEntity.of(orderSDO);
    }

    public static OrderWriteEntity buildOrderEntity(OrderSDO orderSDO) {
        return OrderWriteEntity.of(orderSDO);
    }
}
